﻿Public Class BLLUsuario
    Dim objDAL As DALUsuario
#Region "Variáveis"
    Private codigo As Integer
    Private login As String
    Private senha As String
    Private status As String
    Private data As String
    Private permissao As String
#End Region
#Region "Propriedades"
    Public Property pcodigo As Integer
        Get
            Return Me.codigo
        End Get
        Set(value As Integer)
            Me.codigo = value
        End Set
    End Property
    Public Property pdata As String
        Get
            Return Me.data
        End Get
        Set(ByVal value As String)
            Me.data = value
        End Set
    End Property
    Public Property ppermissao As String
        Get
            Return Me.permissao
        End Get
        Set(ByVal value As String)
            Me.permissao = value
        End Set
    End Property
    Public Property pstatus As String
        Get
            Return Me.status
        End Get
        Set(ByVal value As String)
            Me.status = value
        End Set
    End Property
    Public Property plogin As String
        Get
            Return Me.login
        End Get
        Set(ByVal value As String)
            Me.login = value
        End Set
    End Property
    Public Property psenha As String
        Get
            Return Me.senha
        End Get
        Set(ByVal value As String)
            Me.senha = value
        End Set
    End Property
#End Region
#Region "Métodos"
    Function cadastrar() As Boolean
        Dim ret As Boolean
        Try
            If (senha.Length >= 6) Then
                objDAL = New DALUsuario
                ret = objDAL.CadastrarUsuario(Me.login, Me.senha, Me.status, Me.permissao, Me.data)
            Else
                MessageBox.Show("Senha Não pode ter menos de 6 digitos")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        If ret = True Then
            Return True
        Else
            MessageBox.Show("Usuário ja existente")
            Return False
        End If
    End Function
#Region "Login"
    Function logar() As Boolean
        Dim tab As DataTable
        Try
            objDAL = New DALUsuario
            tab = objDAL.logar(login, senha)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objDAL = Nothing
        End Try
        If (tab.Rows.Count > 0) Then
            pcodigo = tab.Rows(0)("usu_codigo")
            plogin = tab.Rows(0)("usu_login").ToString
            ppermissao = tab.Rows(0)("per_descricao").ToString
            pstatus = tab.Rows(0)("sta_descricao").ToString
            Return True
        Else
            Return False
        End If

    End Function
#End Region
    Function AlterarSenha(ByVal senhaantiga As String) As Boolean
        Dim ret As Boolean
        Try
            'validar alguma regra de negocio
            'se estivesse OK, continuaria,         
            'senao retornava falso e nao continuava
            If (senha.Length >= 6) Then
                objDAL = New DALUsuario
                ret = objDAL.AlterarSenha(Me.senha, senhaantiga, login)
            Else
                MsgBox("Senha não pode ter menos de 6 digitos")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function localizar(ByVal login As String) As DataTable
        Dim tab As DataTable
        Try
            objDAL = New DALUsuario
            tab = objDAL.localizar(login)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return tab
    End Function
    Function Bloquear(ByVal codigo As Integer) As Boolean
        Dim ret As Boolean
        Try
            objDAL = New DALUsuario
            ret = objDAL.Bloquear(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function Ativar(ByVal codigo As Integer) As Boolean
        Dim ret As Boolean
        Try
            objDAL = New DALUsuario
            ret = objDAL.Ativar(codigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Construtores"
    Sub New()

    End Sub
#End Region
End Class
